import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_09_game/config/app_routes.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../widgets/navigation/custom_bottom_navigation.dart';
import 'main_logic.dart';

class MainPage extends StatelessWidget {
  MainPage({super.key});

  final MainLogic logic = Get.put(MainLogic());

  @override
  Widget build(BuildContext context) {
    return PopScope(
        canPop: false,
        onPopInvoked: (bool? didPop) {
      if (didPop == true) {
        return;
      }
      int now = DateTime.now().millisecondsSinceEpoch;
      if (now - logic.lastPopTime > 3000) {
        logic.lastPopTime = now;
        LoadingUtils.showToast(LocaleKeys.exitAppTip.tr);
      } else {
        logic.lastPopTime = 0;
        SystemChannels.platform.invokeMethod('SystemNavigator.pop');
      }
    },
    child:Scaffold(
      body: PageView(
        controller: logic.pageController,
        physics: const NeverScrollableScrollPhysics(),
        pageSnapping: false,
        children: logic.pages,
      ),
      bottomNavigationBar: Obx(() => CustomBottomNavigation(
            currentIndex: logic.currentIndex.value,
            onTap: logic.changeIndex,
          )),

      floatingActionButton: kDebugMode ? _buildFloatingButton(context) : null,
    ));
  }

  Widget _buildFloatingButton(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(bottom: 20.h), // 可根据需要调整底部间距
      child: FloatingActionButton(
        onPressed: () {
          Get.toNamed(AppRoutes.testPage);
          // TODO: 实现浮窗按钮t点击事件
          // 例如：Get.to(SomePage());
        }, // 可替换为自定义图标
        backgroundColor: Colors.blue, // 可自定义颜色
        elevation: 4,
        child: const Icon(Icons.add),
      ),
    );
  }
}
